package com.example.ui.share;

import java.util.ArrayList;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.InviteActivity;
import com.example.ui.learn.RecommendActivity;
import com.example.ui.share.adapter.SRecommendAdapter;
import com.example.ui.sharte.util.SRecommend;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SRecommendActivity  extends Activity{
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private ListView sra_lv;
	private String data;
	private ArrayList<SRecommend> sra_list;
	private SRecommendAdapter sra_adapter;
	private Intent intent;
	private String tid,teacherName;
	private LinearLayout sra_ll,sra_ll1;
	private TextView tvdetail;
	private Button  llgone;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_sr);
		intent=getIntent();
		tid=intent.getStringExtra("blId");
		teacherName=intent.getStringExtra("teacherName");
		 new Thread(sra_run).start(); 
		 sra_list=new ArrayList<SRecommend>();
		init();
	}
	Runnable sra_run = new Runnable(){  
		  
		@Override  
		public void run() {  
		   
		    data=NetRequest.Request_sra(tid);	   
		    sra_list=AnalyzingJson.getcontent_sra(data);
		    h.sendEmptyMessage(1);
		}  
		  }; 
		  private  Handler h=new Handler(){
				 public void handleMessage(android.os.Message msg) {
					 switch (msg.what) {
					case 1:
						 sra_adapter.setCcInfos(sra_list);
						 sra_adapter.notifyDataSetChanged();
						break;
					
					default:
						break;
					}	
				 };				
			};

	/**
	 * 
	 */
	private void init() {
		
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);		
		sra_lv= (ListView)findViewById(R.id.as_lv);
		titleLeftBtn.setText("返回");
		titleText.setText("推荐");
		titleRightBtn.setText("拜访约请");
		tvdetail=(TextView) findViewById(R.id.tvdetail);
		sra_ll=(LinearLayout) findViewById(R.id.as_ll);
		sra_ll1=(LinearLayout) findViewById(R.id.as_rl);
		llgone=(Button) findViewById(R.id.as_return);
		sra_adapter=new SRecommendAdapter(this, sra_list);
		llgone.setOnClickListener(sra_ocl);
		sra_lv.setAdapter(sra_adapter);
		sra_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {				
				tvdetail.setText(sra_list.get(arg2).getContent());
				sra_ll.setVisibility(View.GONE);
				sra_ll1.setVisibility(View.VISIBLE);
			}
		});
		
		titleLeftBtn.setOnClickListener(sra_ocl);
		titleRightBtn.setOnClickListener(sra_ocl);
		
	}
	private OnClickListener sra_ocl=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		
			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:
				Intent cca_l = new Intent();
				cca_l.putExtra("tid",tid);
				cca_l.putExtra("tName", teacherName);
				Log.i("hs recommend. id", tid);
				cca_l.setClass(SRecommendActivity.this, SeeActivity.class);
				startActivity(cca_l);
				break;
			case R.id.as_return:
				sra_ll1.setVisibility(View.GONE);
				sra_ll.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
		}
	};

}
